import { resultSuccess, doCustomTimes, resultPage } from '../_util'

function getMenuKeys() {
  const keys = ['dashboard', 'console', 'workplace', 'basic-form', 'step-form', 'detail']
  const newKeys: string[] = []
  doCustomTimes(parseInt(`${Math.random() * 6}`), () => {
    const key = keys[Math.floor(Math.random() * keys.length)]
    newKeys.push(key)
  })
  return Array.from(new Set(newKeys))
}

const roleList = (pageSize: number) => {
  const result: any[] = []
  doCustomTimes(pageSize, () => {
    result.push({
      id: '@integer(10,100)',
      name: '@cname()',
      explain: '@cname()',
      isDefault: '@boolean()',
      menu_keys: getMenuKeys(),
      create_date: '@date(\'yyyy-MM-dd hh:mm:ss\')',
      'status|1': ['normal', 'enable', 'disable'],
    })
  })
  return result
}

export default [
  {
    url: '/api/system/role',
    timeout: 1000,
    method: 'get',
    response: ({ query }) => {
      const { currentPage = 1, pageSize = 10 } = query
      const list = roleList(Number(pageSize))

      return resultSuccess({
        result: {
          ...resultPage({
            currentPage: Number(currentPage),
            pageSize: Number(pageSize),
          }),
          list,
          total: 60
        }
      })
    },
  },
]
